﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.IO;

namespace YouHP.BLL.MedicalGraph
{
    public class GraphLabcheck : GraphBase
    {
        public List<Brush> mRangeBrush;
        public List<string> mRangeName;
        List<GraphStyle.VariableStyle> mVarStyles;

        public GraphLabcheck()
        {
            mLegendName = "";
            mRangeBrush = new List<Brush>();
            mRangeBrush.Add(new SolidBrush(Color.FromArgb(225, 255, 255)));
            mRangeBrush.Add(new SolidBrush(Color.White));
            mRangeName = new List<string>();
            mRangeName.Add("正常");
            mRangeName.Add("升高");

            mVarStyles = new List<GraphStyle.VariableStyle>();
            GraphStyle.VariableStyle style = new GraphStyle.VariableStyle();
            style.SetColor(Color.Red);
            mVarStyles.Add(style);
            style = new GraphStyle.VariableStyle();
            style.SetColor(Color.FromArgb(255, 153, 0));
            mVarStyles.Add(style);
            style = new GraphStyle.VariableStyle();
            style.SetColor(Color.FromArgb(51, 153, 102));
            mVarStyles.Add(style);
        }
        #region 取得数据在图形上的位置

        protected override double GetDataY(int index, DateTime date)
        {
            return GetDataY(index, 0);
        }
        protected override double GetDataY(int index, double y)
        {
            return _DataRect.Bottom - _DataRect.Height * Rule.GetVariable(index).GetRelPosition2(y);
        }

        protected override void SetupAxis()
        {
            int cnt = Rule.GetVariableCount();
            double per = 1.0f;
            double temp;
            int i;
            for (i = 0; i < cnt; i++)
            {
                temp = Rule.GetVariable(i).GetNormalPosition();
                if (per > temp) per = temp;
            }
            for (i = 0; i < cnt; i++)
            {
                Rule.GetVariable(i).SetNormalPosition(per);
            }
        }

        protected override void DrawData()
        {
            int x1;
            int y1;
            int j;
            int cnt1;
            double val1;
            Rectangle rect1;

            cnt1 = Rule.GetVariableCount();
            int lasty = -1;
            int lastx = -1;
            StringFormat drawformat = new StringFormat();
            drawformat.Alignment = StringAlignment.Center;
            drawformat.LineAlignment = StringAlignment.Center;
            Pen pen = new Pen(Color.Black, 1.0f);
            Font font = new Font("宋体", 9.0f, FontStyle.Bold);
            if (cnt1 > 3) cnt1 = 3;
            for (j = 0; j < cnt1; j++)
            {
                GraphRule.Variable var = Rule.GetVariable(j);
                GraphDataVariableInfo dataVar = Data.GetDataVariable(var.mKey);
                GraphStyle.VariableStyle style = this.mVarStyles[j];
                lastx = lasty = -1;
                foreach (GraphDataItemInfo dataItem in dataVar.DataItemList)
                {
                    x1 = (int)this.GetDataX(0, dataItem.DateTime1);
                    val1 = Utility.Utils.StrToDouble(dataItem.Value, -9999999);
                    if (val1 < -9999998)
                    {
                        lasty = -1;
                        continue;
                    }
                    y1 = (int)this.GetDataY(j, val1);
                    rect1 = new Rectangle(x1 - 2, y1 - 2, 4, 4);
                    _GraphicsData.DrawRectangle(style.mPen, (float)(x1 - 1.5), (float)(y1 - 1.5), 3f, 3f);
                    if (lasty > 0)
                    {
                        _GraphicsData.DrawLine(style.mPen, lastx, lasty, x1, y1);
                    }
                    lasty = y1;
                    lastx = x1;
                }
            }

        }

        protected override void DrawDataGrid()
        {
            int x1, x2;
            int y1, y2;
            int i;

            GraphRule.Variable var = Rule.GetVariable(0);
            Rectangle rect = new Rectangle(0, 0, Rule.DataWidth, Rule.Height);

            for (i = 0; i < MonthCount; i++)
            {
                DateTime dt = _StartDateTime.AddMonths(i);
                x1 = (int)this.GetDataX(0, i + 1);
                y1 = _DataRect.Bottom + 10;
                DrawDataTime(dt, x1, y1);
            }

            y1 = (int)GetDataY(0, var.mUpper[0]);
            y2 = (int)GetDataY(0, var.mLower[0]);
            x1 = (int)GetDataX(0, 0);
            x2 = (int)GetDataX(0, _GridCount);
            _GraphicsData.FillRectangle(mRangeBrush[0], x1, y1, x2 - x1, y2 - y1);

            x1 = rect.Left;
            x2 = rect.Right - 10;
            y1 = (int)this.GetDataY(0, 0.0);
            _GraphicsData.DrawLine(Rule.mXAxisPen, x1, y1, x2, y1);

            

            y1 = this._DataRect.Top;
            y2 = this._DataRect.Bottom;
            for (i = 1; i <= _GridCount; i++)
            {
                x1 = (int)this.GetDataX(0, i);
                _GraphicsData.DrawLine(Rule.mGridPen, x1, y1, x1, y2);
                _GraphicsData.DrawString(i.ToString(),
                    Rule.mGridFont, Brushes.Black, x1, y2, Rule.mLabelFotmat);
            }

        }

        protected override void SetupPage()
        {


            DateTime dt = DateTime.Now;
            string timestr = dt.ToString("yyyyMMddhhmmss");
            mDataPath = Path.Combine(SavePath, string.Format("Lab_{1}_Data_{0}.png", Suffix, timestr));
            mLegendPath = Path.Combine(SavePath, string.Format("Lab_{1}_Legend_{0}.png", Suffix, timestr));
            mWebDataPath = Path.Combine(WebPath, string.Format("Lab_{1}_Data_{0}.png", Suffix, timestr));
            mWebLegendPath = Path.Combine(WebPath, string.Format("Lab_{1}_Legend_{0}.png", Suffix, timestr));
            Result.GraphWebPath += "|" + mWebLegendPath;
            Result.GraphWebPath += "|" + mWebDataPath;

            dt = Data.MinDateTime;
            _StartDateTime = new DateTime(dt.Year, dt.Month, 1);
            dt = Data.MaxDateTime.AddMonths(1);
            _EndDateTime = new DateTime(dt.Year, dt.Month, 1);
            MonthCount = (Data.MaxDateTime.Year - Data.MinDateTime.Year) * 12;
            MonthCount += (Data.MaxDateTime.Month - Data.MinDateTime.Month);
            _GridCount = MonthCount;
            if (_GridCount < 15) _GridCount = 15;
            Rule.DataWidth = _GridCount * 30 + 30;
         
           
            //mParam.mLegendWidth = mParam.mVariables.Count * 50 + 200;
            _DataRect = new Rectangle(0, 30, Rule.DataWidth, Rule.Height - 60);
 
        }

        protected override void DrawLegend()
        {

            int i;
            int x1, x2;
            int y1;
            Pen pen1;
            Font font1;
            Rectangle rect1;
            int cnt1;
            StringFormat dformat;

            GraphRule.Variable var;
            Graphics g = this.mGraphicsLegend;
            dformat = new StringFormat();
            dformat.Alignment = StringAlignment.Far;
            dformat.LineAlignment = StringAlignment.Far;
            font1 = new Font("宋体", 9.0f);
            x1 = 40;
            x2 = 150;
            cnt1 = Rule.GetVariableCount();
            pen1 = new Pen(Color.Black, 1.0f);
            y1 = 30;
            g.DrawRectangle(pen1, x1 - 10, y1, 125, cnt1 * 40 + 10);
            y1 += 10;
            g.DrawString(mLegendName, font1, Brushes.Black, x1, y1);
            y1 += 20;
            for (i = 0; i < cnt1; i++)
            {
                var = Rule.GetVariable(i);
                g.DrawString(var.mName, font1, Brushes.Black, x2, y1, dformat);
                y1 += 15;
                rect1 = new Rectangle(x1, y1, 50, 15);
                g.DrawString(var.Unit, font1, Brushes.Black, x2, y1, dformat);
                y1 += 25;
            }

            var = Rule.GetVariable(0);
            cnt1 = var.mLower.Count;
            g.DrawRectangle(pen1, x1 - 10, y1 - 8, 125, cnt1 * 20 + 10);
            for (i = 0; i < cnt1; i++)
            {

                rect1 = new Rectangle(x1, y1, 50, 15);
                g.FillRectangle(mRangeBrush[cnt1 - 1 - i], rect1);
                g.DrawRectangle(pen1, rect1);
                g.DrawString(mRangeName[cnt1 - 1 - i], font1, Brushes.Black, x1 + 90, y1 + 17, dformat);
                y1 += 20;

            }

            /*
            Rectangle rect = new Rectangle(10, 10, mParam.mLegendWidth - 20, mParam.mHeight - 20);
            int cnt1, cnt2;
            int i, j, k;
            int x1, x2, x3;
            int y1, y2, y3;
            Pen pen1, pen2;
            Font font1, font2;
            Rectangle rect1, rect2;
            StringFormat dformat = new StringFormat();
            double val;

            this.mDataRect = new Rectangle(240, rect.Top + 20, mParam.mLegendWidth, mParam.mHeight - 60);
            GraphVariable var = mParam.mVariables[0];
            cnt1 = var.mLower.Count;
            pen1 = new Pen(Color.Black, 1.0f);
            x1 = 20;
            y1 = rect.Bottom - 40;

            float width = 0;
            int height = 5;

            Graphics g = mGraphicsLegend;
            

            dformat.Alignment = StringAlignment.Near;
            dformat.LineAlignment = StringAlignment.Center;
            font1 = new Font("宋体", 9.0f);
            x2 = x1 + 60;
            for (i = 0; i < cnt1; i++)
            {

                y2 = y1 + 10;
                rect1 = new Rectangle(x1, y1, 50, 15);
                g.FillRectangle(var.mRangBrush[i], rect1);
                g.DrawRectangle(pen1, rect1);
                g.DrawString(var.mRangeName[i], font1, Brushes.Black, x2, y2, dformat);
                y1 -= 20;

            }
            x1 = 10;
            y1 += 20;
            y1 -= 6;
            height = 20 * cnt1 + 6;
            g.DrawRectangle(pen1, x1, y1, 170, height);

            cnt1 = mParam.mVariables.Count;
            y2 = y1;
            y1 -= 10;
            x1 = 170;
            x2 = 20;
            dformat.Alignment = StringAlignment.Far;
            dformat.LineAlignment = StringAlignment.Far;
            for (i = 0; i < cnt1; i++)
            {
                var = mParam.mVariables[cnt1 - 1 - i];
                dformat.Alignment = StringAlignment.Far;
                g.DrawString(var.mUnit, var.mFont2, var.mBrush, x1, y1, dformat);
                y1 -= 20;
                dformat.Alignment = StringAlignment.Far;
                g.DrawString(var.mName, var.mFont, var.mBrush, x1, y1, dformat);
                y1 -= 30;
            }
            x1 = 10;
            height = 45 * cnt1 + 18;
            g.DrawRectangle(pen1, x1, y1, 170, height);

            for (i = 0; i < cnt1; i++)
            {
                var = mParam.mVariables[i];
            }
             * */
        }

        protected override void DrawLegendGrid()
        {
            int i, j;
            int x1 ;
            int y1, y2;
            Pen pen1;
            Font font1;
            int cnt1, cnt2;
            StringFormat dformat;
            double val;


            int left = Rule.LegendWidth - 75;
            x1 = left;
            GraphRule.Variable var = Rule.GetVariable(0);
            y1 = (int)GetDataY(0, var.mUpper[0]);
            y2 = (int)GetDataY(0, var.mLower[0]);
            this.mGraphicsLegend.FillRectangle(mRangeBrush[0], x1, y1, _DataRect.Right, y2 - y1);
            pen1 = new Pen(Color.Black, 2.0f);
            mGraphicsLegend.DrawLine(pen1, x1, y2, _DataRect.Right, y2);
            font1 = new Font("宋体", 9.0f);
            cnt1 = Rule.GetVariableCount();
            dformat = new StringFormat();
            dformat.Alignment = StringAlignment.Far;
            dformat.LineAlignment = StringAlignment.Center;
            for (i = 0; i < cnt1; i++)
            {
                GraphStyle.VariableStyle style = this.mVarStyles[i];
                var = Rule.GetVariable(i);
                x1 = left + 35 * i;
                y1 = _DataRect.Bottom;
                y2 = _DataRect.Top;
                mGraphicsLegend.DrawLine(style.mAxisPen, x1, y1, x1, y2);
                cnt2 = var.GridValue.Count - 1;
                for (j = 0; j < cnt2; j++)
                {
                    val = var.GridValue[j];
                    y1 = (int)GetDataY(i, val);
                    mGraphicsLegend.DrawLine(style.mPen, x1, y1, x1 + 5, y1);
                    mGraphicsLegend.DrawString(val.ToString(), font1, Brushes.Black, x1 - 2, y1, dformat);
                }
                y1 = (int)GetDataY(i, var.GridValue[cnt2]);
                mGraphicsLegend.DrawString(var.ShortName, font1, Brushes.Black, x1 - 2, y1, dformat);

            }

            left = Rule.LegendWidth - 75;
            x1 = left;
            var = Rule.GetVariable(0);
            y1 = (int)GetDataY(0, var.mUpper[0]);
            y2 = (int)GetDataY(0, var.mLower[0]);
            //this.mGraphicsLegend.FillRectangle(var.mRangBrush[0], x1, y1, mDataRect.Right, y2 - y1);
            pen1 = new Pen(Color.Black, 2.0f);
            mGraphicsLegend.DrawLine(pen1, x1, y2, _DataRect.Right, y2);
        }

        #endregion
    } 
}
